/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.CommonStoreActivity;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.HomeTop;
import com.epfresh.bean.HomeTopBean;
import com.epfresh.utils.TimeStringUtils;
import com.umeng.analytics.MobclickAgent;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class ViewBinderHomeStoreOne
        extends ItemViewBinder<HomeTopBean.StoreOne, ViewBinderHomeStoreOne.ViewHolder> implements View.OnClickListener {
    int imageWidth;
    int tagImageWidth;
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
        if (o instanceof HomeTopBean.StoreOne) {
            HomeTopBean.StoreOne bean = ((HomeTopBean.StoreOne) o);
            MobclickAgent.onEvent(v.getContext(), "Click_Click_HomeRecommend");/*统计广告点击*/
            Intent storeIntent = new Intent(v.getContext(),
                    CommonStoreActivity.class);
            storeIntent.putExtra("storeId", bean.store.getId());
            v.getContext().startActivity(storeIntent);
        } else {
            HomeTopBean.FreshestBean.StoreBean.First3Bean bean = (HomeTopBean.FreshestBean.StoreBean.First3Bean) o;
            MobclickAgent.onEvent(v.getContext(), "Click_Click_HomeRecommend");/*统计广告点击*/
            Intent commIntent = new Intent(v.getContext(),
                    DetailGoodsActivity.class);
            Log.e("id____", bean.getId());
            commIntent.putExtra("storeId", bean.getStoreId());
            commIntent.putExtra("id", bean.getId());
            commIntent.putExtra("image_url", bean.getThumbnail());
            //commIntent.putExtra("promotionItemId", bean.store.getFirst3().get(0).getPromotionItemId());
            commIntent.putExtra("title", bean.getTitle());
            v.getContext().startActivity(commIntent);
        }
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.view_home_store_one, parent, false);
        imageWidth = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(52)) / 3;
        tagImageWidth = imageWidth / 3;
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeTopBean.StoreOne bean) {
        ViewGroup.LayoutParams params = holder.iv_product.getLayoutParams();
        params.width = imageWidth;
        params.height = (int)(imageWidth*0.75);
        holder.iv_product.setLayoutParams(params);
        ViewGroup.LayoutParams tagParams = holder.iv_tag.getLayoutParams();
        tagParams.width = tagImageWidth;
        tagParams.height = tagImageWidth;
        HomeTopBean.FreshestBean.StoreBean store = bean.store;
        if(store==null)
            return;
        holder.tv_title.setText(bean.store.getName());
        if (bean.store.getStall() == null || "".equals(bean.store.getStall())) {
            bean.store.setStall("临时店铺");
        }
        holder.tv_store_info.setText(bean.store.getMarketName() + "  " + bean.store.getStall());
        holder.tv_store_time.setText(TimeStringUtils.initTime(bean.store.getTime(), null));
        holder.itemView.setTag(R.id.item_key_status, bean);
        holder.itemView.setOnClickListener(this);
        if(store==null||store.getFirst3()==null||store.getFirst3().size()==0){
            ImageLoader.getInstance().displayImage("", holder.iv_product);
            return;
        }
        holder.iv_product.setTag(R.id.item_key_status, bean.store.getFirst3().get(0));
        holder.iv_product.setOnClickListener(this);
            ImageLoader.getInstance().displayImage(bean.store.getFirst3().get(0).getThumbnail(), holder.iv_product);
        if (store.getFirst3().get(0).getPromotionMarkUrl() != null && !"".equals(store.getFirst3().get(0).getPromotionMarkUrl()) && !"null".equals(store.getFirst3().get(0).getPromotionMarkUrl())){
            ImageLoader.getInstance().displayImage(bean.store.getFirst3().get(0).getPromotionMarkUrl(), holder.iv_tag);
            holder.iv_tag.setVisibility(View.VISIBLE);
        } else
            holder.iv_tag.setVisibility(View.INVISIBLE);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_product, iv_tag;//iv_type;title + drawable? 预售？
        TextView tv_title, tv_store_info, tv_store_time;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_product = (ImageView) itemView.findViewById(R.id.iv_product);
            iv_tag = (ImageView) itemView.findViewById(R.id.iv_tag);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_store_info = (TextView) itemView.findViewById(R.id.tv_store_info);
            tv_store_time = (TextView) itemView.findViewById(R.id.tv_store_time);
        }
    }
}
